package com.chengh.vhr.service.serviceImpl;

import com.chengh.vhr.common.myEnum.ErrorCodeEnum;
import com.chengh.vhr.exception.MyException;
import com.chengh.vhr.mapper.DepartmentMapper;
import com.chengh.vhr.model.Department;
import com.chengh.vhr.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chengh
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartments() {
        List<Department> departments = departmentMapper.getAllDepartments();

        //利用Stream方法进行过滤
        return departments.stream().filter(department ->
                department.getParentId() == -1//返回父目录为-1的就是一级分类
        ).peek(item -> item.setChildren(getChildren(item, departments))).collect(Collectors.toList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addDepartment(Department department) {
        department.setIsParent(false);
        department.setEnabled(true);

        // 1.先添加
        departmentMapper.save(department);
        // 2.更新数据
        department.setDepPath(department.getDepPath() + "." + department.getId());
        departmentMapper.updateByPrimaryKeySelective(department);
        // 3.判断是否父节点在添加是不是父节点,如果不是,修改
        if(!department.getIsParent()) {
            Department department1 = new Department();
            department1.setId(department.getParentId());
            department1.setIsParent(true);
            departmentMapper.updateByPrimaryKeySelective(department1);
        }
        return department.getId();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteDepartmentByIds(List<Integer> ids) {
        if(ids != null && ids.size() == 1) {
            // 查看是否有子部门
            List<Integer> idsByParentIds = departmentMapper.getIdsByParentIds(ids);
            // 有字部门时直接返回
            if(idsByParentIds != null && idsByParentIds.size() > 0) {
                throw new MyException(ErrorCodeEnum.CUSTOM_EXCEPTION_102.getMsg(),ErrorCodeEnum.CUSTOM_EXCEPTION_102.getCode());
            }
        }
        // 1.查询将要删除部门的父部门id
        List<Integer> parentIds = departmentMapper.getParentId(ids);

        // 2.删除
        int i = departmentMapper.deleteDeprmentByIds(ids);
        if(i == ids.size()) {
            // 3.查看父部门是否有字部门
            List<Integer> hasChildrenIds = departmentMapper.getIdsByParentIds(parentIds);

            // 4.取差集(没有字部门的需要改变isParent)
            List<Integer> noChildrenIds = parentIds.stream().filter(item -> !hasChildrenIds.contains(item)).collect(Collectors.toList());

            // 5.更新数据
            return departmentMapper.updateIsParentByIds(noChildrenIds) == noChildrenIds.size();
        }

        return false;
    }

    /**
     * @Description 不做分级，获得所有的部门列表
     * @return java.util.List<com.chengh.vhr.model.Department>
     * @Author chengh
     * @Datetime 2021/1/25 20:43
     */
    @Override
    public List<Department> getDepartments() {
        return departmentMapper.getDepartments();
    }

    private List<Department> getChildren(Department root,List<Department> all) {
        return all.stream().filter(department ->
            department.getParentId().equals(root.getId())).peek(item ->
                item.setChildren(getChildren(item, all))).collect(Collectors.toList());
    }
}
